import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

//用来判定是否为cookie三天内免登录
public class IfLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有的cookie
        Cookie[] cookies=request.getCookies();
        String username=null;
        String password=null;
        //判断cookie是否为null
        if(cookies!=null){
            //遍历cookies
           for (Cookie cookie:cookies){
               //取出cookie的名字和值
               String cookieName=cookie.getName();
               String cookieValue=cookie.getValue();
               if ("username".equals(cookieName)){
                   username=cookieValue;
                }else if("password".equals(cookieName)){
                    password=cookieValue;
               }
           }
        }
        if(username!=null && password!=null){
            Connection conn=null;
            PreparedStatement ps=null;
            ResultSet rs=null;
            boolean loginSuccess=false;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn= DriverManager.getConnection("jdbc:mysql://localhost/jcdb","root","19970913Xun");
                String sql="select username,password from t_user where username=? and password=?";
                ps=conn.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);
                rs=ps.executeQuery();
                if(rs.next()){
                    loginSuccess=true;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
            if (loginSuccess) {
                response.sendRedirect(request.getContextPath() + "/Main.html");
            }else{
                response.sendRedirect(request.getContextPath() + "/LoginError.html");
                }
            }else{
                response.sendRedirect(request.getContextPath()+"/login.html");
        }
    }
}


